﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace iData
{
    public class IO
    {
        private static string GetCurrentPath()
        {
            return System.Web.HttpContext.Current.Server.MapPath(@"~");
        }

          /// <summary>
        /// 写入执行日志
        /// </summary>
        public static void WriteExecuteLog(string Content)
        {
            string strDate = DateTime.Today.ToString("yyyyMMdd");
            //string currentProgramName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
            string FileSavePath = GetCurrentPath() + "Log" + strDate + ".txt";
            Content = DateTime.Now.ToString() + " " + Content + "\n\r";
            //if (!Directory.Exists("log")) Directory.CreateDirectory("log");
            //if (!File.Exists(FileSavePath)) File.Create(FileSavePath);
            FileStream fileStream = new FileStream(FileSavePath, FileMode.Append);
            StreamWriter sw = new StreamWriter(fileStream);// File.AppendText(FileSavePath);

            sw.WriteLine(Content);
            sw.Flush();
            sw.Close();
            fileStream.Close();
            sw = null;
            //Console.WriteLine(Content);
            
        }

        
        ////写入文件    
        //private static void WriteFile(string Content, string FileSavePath)
        //{
            
        //    if (System.IO.File.Exists(FileSavePath))
        //    {
        //        System.IO.File.Delete(FileSavePath);
        //    }
        //    System.IO.FileStream fs = System.IO.File.Create(FileSavePath);
        //    Byte[] bContent = System.Text.Encoding.GetEncoding("gb2312").GetBytes(Content);
        //    fs.Write(bContent, 0, bContent.Length);
        //    fs.Close();
        //    fs = null;
        //}

        ////读取文件  
        private static string ReadFile(string FilePath)
        {
            System.IO.StreamReader rd = System.IO.File.OpenText(FilePath);
            string StrRead = rd.ReadToEnd().ToString();
            rd.Close();
            return StrRead;
        }

        public static string ReadConnstr()
        {
            string FilePath = GetCurrentPath() + "\\connstr.txt";
            System.IO.StreamReader rd = System.IO.File.OpenText(FilePath);
            //string StrRead = rd.ReadToEnd().ToString();
            string StrRead = rd.ReadLine().ToString();
            rd.Close();
            return StrRead;
        }
        public static string ReadConnstrMysql()
        {
            string FilePath = GetCurrentPath() + "\\connstrMysql.txt";
            System.IO.StreamReader rd = System.IO.File.OpenText(FilePath);
            //string StrRead = rd.ReadToEnd().ToString();
            string StrRead = rd.ReadLine().ToString();
            rd.Close();
            return StrRead;
        }

    }
}
